﻿using System;
using System.Windows.Forms;
using UIFileExplorerProgress;

namespace AntivirusFriendsSusu
{
    using System.Collections.Generic;

    public partial class MainForm : Form, IExplorerProgressView
    {
        private readonly IPresenter _presenter;

        public MainForm()
        {
            InitializeComponent();

            _presenter = new DefaultPresenter(this);
        }

        private void OnFileExplorerBeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes[0].Text == "")
            {
                var node = _presenter.FileExplorer.EnumerateDirectory(e.Node);
            }   
        }

        private void OnMainFormLoad(object sender, EventArgs e)
        {
            _presenter.FileExplorer.CreateTree(FileExplorer);
        }
        
        private void OnFileExplorerNodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            listTargets.Items.Add(FileExplorer.SelectedNode.FullPath);
            if (listTargets.Items.Count > 0)
                Start.Enabled = true;
        }

        private void OnDeleteTargetClick(object sender, EventArgs e)
        {
            if(listTargets.SelectedItems.Count>0)
                listTargets.Items[listTargets.SelectedIndices[0]].Remove();

            if (listTargets.Items.Count < 1)
                Start.Enabled = false;
        }

        private void OnListTargetsItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (listTargets.SelectedItems.Count > 0)
                DeleteTarget.Enabled = true;
            else DeleteTarget.Enabled = false;
        }

        private void OnStopClick(object sender, EventArgs e)
        {
            _presenter.StopScan();
        }

        private void OnStartClick(object sender, EventArgs e)
        {
            var targets = new List<string>();
            var targetsCount = listTargets.Items.Count;
            for (var i = 0; i < targetsCount; i++)
                targets.Add(listTargets.Items[i].Text);

            _presenter.StartScan(targets);
        }

        #region IExplorerProgressView Members

        public void InitializeProgressBar(int maxValue)
        {
            if (ProgressScan.InvokeRequired)
            {
                ProgressScan.Invoke(
                    new MethodInvoker(() =>
                                          {
                                              ProgressScan.Step = 1;
                                              ProgressScan.Value = 0;
                                              ProgressScan.Maximum = maxValue;
                                          }));
            }

            if (CountingProgress.InvokeRequired)
            {
                CountingProgress.Invoke(
                    new MethodInvoker(() =>
                                          {
                                              CountingProgress.Text = "0";
                                          }));
            }

            ProgressBarInitialized = true;
        }

        public void ResetProgressBar()
        {
            if (ProgressScan.InvokeRequired)
            {
                ProgressScan.Invoke(
                    new MethodInvoker(() =>
                                          {
                                              ProgressScan.Step = 1;
                                              ProgressScan.Value = 0;
                                              ProgressScan.Maximum = 0;
                                          }));
            }

            if (CountingProgress.InvokeRequired)
            {
                CountingProgress.Invoke(
                    new MethodInvoker(() =>
                                          {
                                              CountingProgress.Text = "0";
                                          }));
            }

            ProgressBarInitialized = false;
        }

        public void ProgressBarStep()        
        {
            MethodInvoker progressScanMethodInvoker = () =>
            {
                ProgressScan.PerformStep();

                if (ProgressScan.Value == ProgressScan.Maximum)
                    ProgressBarInitialized = false;
            };

            if (ProgressScan.InvokeRequired)
                ProgressScan.Invoke(new MethodInvoker(progressScanMethodInvoker));
            else
                progressScanMethodInvoker();

            MethodInvoker countingProgressMethodInvoker = () =>
                                                              {
                                                                  var percentage = ProgressScan.Value*100.0/
                                                                                   ProgressScan.Maximum;
                                                                  CountingProgress.Text = percentage.ToString("0.##");
                                                              };

            if (CountingProgress.InvokeRequired)
                CountingProgress.Invoke(
                    new MethodInvoker(countingProgressMethodInvoker));
            else
                countingProgressMethodInvoker();
        }

        public bool ProgressBarInitialized { get; private set; }

        public void AddInfectedFile(string filename, string virusName)
        {
            var item = new ListViewItem(new[] { filename, virusName });
            MethodInvoker infectedFilesMethodInvoker = () => listViewInfectedFiles.Items.Add(item);

            if (listViewInfectedFiles.InvokeRequired)
                listViewInfectedFiles.BeginInvoke(new MethodInvoker(infectedFilesMethodInvoker));
            else
                infectedFilesMethodInvoker();
        }

        public void ClearInfectedFiles()
        {
            listViewInfectedFiles.Items.Clear();
        }

        #endregion
    }
}
